package casa.depesos.app.com.activity;

import android.text.TextUtils;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.Choose2Dlg;
import casa.depesos.app.com.dlg.ChooseDlg;
import casa.depesos.app.com.http.data.City;
import casa.depesos.app.com.http.data.Me;
import casa.depesos.app.com.http.data.Province;
import casa.depesos.app.com.view.MyTextView;

public abstract class BaseChooseActivity extends BaseActivity {

    @Override
    protected boolean needPadding() {
        return false;
    }

    protected void chooseValue(TextView tv, int id, Object map, String key, String title) {
        List<String> list = Arrays.asList(getResources().getStringArray(id));
        chooseValue(tv, list, map, key, title);
    }

    protected void chooseValue(TextView tv, List list, Object map, String key, String title) {
        chooseValue(tv, list, map, key, title, 0);
    }

    protected void chooseValue(TextView tv, List list, Object map, String key, String title, final int start) {
        tv.setOnClickListener(view -> {
            new ChooseDlg().setData(list, tv.getText().toString(), title).setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        int tmp = (Integer) value + 1 + start;
                        setValue(map, key, tmp);
                        String old = tv.getText().toString();
                        tv.setText(list.get((Integer) value).toString());
                        if (tv instanceof MyTextView) {
                            String now = tv.getText().toString();
                            if (!TextUtils.equals(old, now)) {
                                ((MyTextView) tv).onTextChange(tmp);
                            }
                        }
                    }
                }
            }).show(getSupportFragmentManager(), "");
        });
    }

    protected void choose2Value(TextView tv, List<Province> list, Me map, String title) {
        tv.setOnClickListener(view -> {
            int[] p = getDefaultArea(map);
            new Choose2Dlg().setData(list, p[0], p[1], title).setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk && value instanceof Map) {
                        String s = ((Map) value).get("text").toString();
                        tv.setText(s);
                        int p = (Integer) ((Map<?, ?>) value).get("province");
                        int c = (Integer) ((Map<?, ?>) value).get("city");
                        City tmp = list.get(p).list.get(c);
                        map.live = tmp.provinceCode + "," + tmp.code;
                        map.liveDes = s;
                        check();
                    }
                }
            }).show(getSupportFragmentManager(), "");
        });
    }

    private void setValue(Object obj, String key, Object value) {
        try {
            Field f = obj.getClass().getDeclaredField(key);
            f.set(obj, value);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private int[] getDefaultArea(Object map) {
        int group, child;
        if (map instanceof Me) {
            try {
                String[] tmp = ((Me) map).live.split(",");
                group = Integer.valueOf(tmp[0]);
                child = Integer.valueOf(tmp[1]);
            } catch (Throwable e) {
                group = -1;
                child = -1;
            }
        } else {
            group = -1;
            child = -1;
        }
        return new int[]{group, child};
    }

    protected abstract void check();
}
